package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import com.base.common.app.LoginUtils;
import com.base.common.model.http.err.ApiException;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;


import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityMoneyTixianBinding;
import app.fugema.shop.main.center.model.CheckMethodResult;
import app.fugema.shop.main.center.vm.UserVM;

public class TiXianActivity extends BaseTitleActivity<ActivityMoneyTixianBinding, UserVM>
{

    private String userKey;

    public static void start(Context context)
    {
        Intent intent = new Intent(context, TiXianActivity.class);
        //intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_tixian);
    }

    private int applyType;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("提现",R.color.white);

        binding.typeRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                 switch (checkedId)
                 {
                     case R.id.radio_alipay:
                         //选择 阿里提现
                         applyType = 3;

                         break;
                     case R.id.radio_weixin:
                         //选择 微信提现
                         applyType = 2;

                         break;
                 }
            }
        });


        binding.btnTixian.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                String moneyStr = binding.etTixianMoney.getText().toString();
                String pwd = binding.etPayPwd.getText().toString();
                String openId = binding.etAccount.getText().toString();
                String nickName = binding.etRealName.getText().toString();

                if(UIUtils.isEmpty(moneyStr) ||UIUtils.isEmpty(pwd)||UIUtils.isEmpty(openId)||UIUtils.isEmpty(nickName))
                {
                    UIUtils.showToastSafesClose("不可为空");
                    return;
                }
                int money = Integer.parseInt( moneyStr );

                viewModel.appleyDarwMoney(openId,money,nickName,pwd,applyType,userKey )
                         .observe(TiXianActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>() {
                             @Override
                             protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                             {
                                 //{"success":false,"msg":"提现金额不能超出可用金额！","code":18}
                                    if( data.data.isSuccess() )
                                    {
                                        UIUtils.showToastSafes("已提交申请");
                                        finish();
                                    }
                                    else
                                    {
                                        UIUtils.showToastSafesClose(data.data.getMsg());
                                    }
                             }

                             @Override
                             protected void onAppError(ApiException error)
                             {
                                 UIUtils.showToastSafesClose(error.getMessage());
                             }

                             @Override
                             protected void onError(ApiException error)
                             {
                                 UIUtils.showToastSafesClose(error.getMessage());
                             }
                         });

            }
        });

    }
    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        //id = intent.getIntExtra("id", 0);
        userKey = LoginUtils.getToken();

        viewModel.checkTiXianMethod(userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<CheckMethodResult>>()
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<CheckMethodResult> data)
                     {
                          if( data.data.isSuccess() )
                          {

                              if( data.data.isCanAlipay() )
                              {
                                  binding.radioAlipay.setVisibility(View.VISIBLE);
                                  binding.radioAlipay.setChecked(true);
                                  applyType = 3;
                              }
                              else
                              {
                                  binding.radioAlipay.setVisibility(View.GONE);
                              }

                              if( data.data.isCanWeiXin() )
                              {
                                  binding.radioWeixin.setVisibility(View.VISIBLE);
                              }
                              else
                              {
                                  binding.radioWeixin.setVisibility(View.GONE);
                              }
                              // 只有微信时
                              if( !data.data.isCanAlipay() && data.data.isCanWeiXin() )
                              {
                                  applyType = 2;
                              }
                          }
                     }
                 });

    }

}


